package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IPermissionService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleservice ;

    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ISystemMenuService systemMenuService;


    @RequestMapping("/list")
    @RequiredPermission("角色列表")
    public String list(Model model ,QueryObject qo){

        PageResult pageResult = roleservice.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "role/list";
    }
    @RequiredPermission("角色删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        System.out.println("123");
        try {
            roleservice.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return  JSONResult.success("删除成功");
    }

    @RequestMapping("/input")
    @RequiredPermission("角色编辑")
    public String input(Model model,Long id){
       model.addAttribute("permissions", permissionService.selectAll());
        model.addAttribute("menus", systemMenuService.selectAll());
        if(id != null){
            model.addAttribute("role",roleservice.selectByPrimaryKey(id));
        }
        return "role/input";

    }


    @RequiredPermission("角色添加或删除")
    @RequestMapping("/saveorupdate")
    @ResponseBody
    public JSONResult  saveorupdate(Role role ,Long[] ids,Long[] menuIds){
        System.out.println(ids);
        try {
            roleservice.saveorupdate(role ,ids,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark("操作失败!") ;
        }
        return JSONResult.success("操作成功!") ;
    }

}
